#+TITLE:Javascript Configuration
#+AUTHOR:Joshua Branson
#+LATEX_HEADER: \usepackage{lmodern}
#+LATEX_HEADER: \usepackage[QX]{fontenc}

This files contains my various javascript settings.

* js2 mode
:PROPERTIES:
:ID:       6427284b-565d-418a-a57a-995f36b1175d
:END:
** js syntax
:PROPERTIES:
:ID:       2eaabd4c-b506-4077-aefa-3ce1cec0f8a7
:END:

#+BEGIN_SRC emacs-lisp
  (use-package js2-mode

    :config
    ;; Disable js2 mode's syntax error highlighting by default...
    (setq-default js2-mode-show-parse-errors nil
                  js2-mode-show-strict-warnings nil)
    ;; ... but enable it if flycheck can't handle javascript
    (autoload 'flycheck-get-checker-for-buffer "flycheck")

    (defun sanityinc/disable-js2-checks-if-flycheck-active ()
      (unless (flycheck-get-checker-for-buffer)
        (set (make-local-variable 'js2-mode-show-parse-errors) t)
        (set (make-local-variable 'js2-mode-show-strict-warnings) t)))
    (add-hook 'js2-mode-hook 'sanityinc/disable-js2-checks-if-flycheck-active)

    (add-hook 'js2-mode-hook (lambda () (setq mode-name "JS2")))

    ;; (defvar preferred-javascript-indent-level 2)

    (setq-default
     js2-basic-offset 2
     js2-bounce-indent-p nil)
    (js2-imenu-extras-setup)

    :mode
    ("\\.js?\\'" . js2-mode))


#+END_SRC

** js2-mode-hook
:PROPERTIES:
:ID:       1f024860-16b8-4e48-ae68-358ac629c0fa
:END:

#+BEGIN_SRC emacs-lisp
  (defun my-js-minify-function ()
    "Minifying my js files."
    (interactive)
    (when (eq major-mode 'js2-mode)
      (async-shell-command (concat (format "closure --js  %s --js_output_file "
                                           (buffer-file-name))
                                   (s-replace ".js" ".min.js" buffer-file-name)) "*js minifying*")))

  (add-hook 'js2-mode-hook '(lambda ()
                              ;; I have abbrev turned on for all prog and text modes
                              ;; (abbrev-mode 1)
                              ;; (skewer-mode)
                              (ggtags-mode 1)
                              ;;(push '("function" . ?𝆑) prettify-symbols-alist)
                              (push '(">=" . ?≥) prettify-symbols-alist)
                              (push '("<=" . ?≤) prettify-symbols-alist)
                              (add-hook 'after-save-hook 'my-js-minify-function nil t)))

  (add-to-list 'display-buffer-alist (cons "\\\*js minifying\\\*" (cons #'display-buffer-no-window nil)))
#+END_SRC

** COMMENT prefeered javascript

Need to first remove from list if present, since elpa adds entries too, which
may be in an arbitrary order
#+BEGIN_SRC emacs-lisp
(eval-when-compile (require 'cl))

(setq auto-mode-alist (cons `("\\.js\\(\\.erb\\)?\\'" . ,preferred-javascript-mode)
                            (loop for entry in auto-mode-alist
                                  unless (eq preferred-javascript-mode (cdr entry))
                                  collect entry)))
#+END_SRC
* inferior js
:PROPERTIES:
:ID:       3fc76131-9245-40ad-83c1-391361570403
:END:
#+BEGIN_SRC emacs-lisp

(setq inferior-js-program-command "js")

(defvar inferior-js-minor-mode-map (make-sparse-keymap))
(define-key inferior-js-minor-mode-map "\C-x\C-e" 'js-send-last-sexp)
(define-key inferior-js-minor-mode-map "\C-\M-x" 'js-send-last-sexp-and-go)
(define-key inferior-js-minor-mode-map "\C-cb" 'js-send-buffer)
(define-key inferior-js-minor-mode-map "\C-c\C-b" 'js-send-buffer-and-go)
(define-key inferior-js-minor-mode-map "\C-cl" 'js-load-file-and-go)

(define-minor-mode inferior-js-keys-mode
  "Bindings for communicating with an inferior js interpreter."
  nil " InfJS" inferior-js-minor-mode-map)

(dolist (hook '(js2-mode-hook js-mode-hook))
  (add-hook hook 'inferior-js-keys-mode))
#+END_SRC

* COMMENT Things I don't use
** COMMENT Coffeescript

#+BEGIN_SRC emacs-lisp
(use-package coffee-mode
  :defer t
  :config (setq coffee-js-mode preferred-javascript-mode
                coffee-tab-width preferred-javascript-indent-level))

(when (fboundp 'coffee-mode)
  (add-to-list 'auto-mode-alist '("\\.coffee\\.erb\\'" . coffee-mode)))
#+END_SRC

** COMMENT js-mode
#+BEGIN_SRC emacs-lisp

(setq-default js-indent-level preferred-javascript-indent-level)

(add-to-list 'interpreter-mode-alist (cons "node" preferred-javascript-mode))
#+END_SRC

Javascript nests {} and () a lot, so I find this helpful

* provide this file
:PROPERTIES:
:ID:       acca9101-ebf5-4d01-bf86-de55c8600a0c
:END:

#+BEGIN_SRC emacs-lisp
(provide 'init-javascript)
#+END_SRC
